# coding:utf-8

from datetime import date

from sqlalchemy import text

from app.extensions import db
from app.models import BaseModel


class WarZone(BaseModel):
    """
    created by chenhj at 2017/12/15
    战区表
    """
    __tablename__ = 'war_zone'

    admin_id = db.Column(db.ForeignKey('admin.id'))
    name = db.Column(db.String(512), default='', unique=True)

    admin = db.relationship('AdminUser')

    areas = db.relationship(
        'Area',
        lazy='dynamic',
        backref=db.backref('war_zone')
    )

    zone_users = db.relationship(
        'User',
        lazy='dynamic',
        backref=db.backref('war_zone')
    )

    @property
    def areas_display(self):
        area_names = [x.name for x in self.areas]
        return ','.join(area_names)


class WarZoneDailyStatic(BaseModel):
    """
    create by ld_wu 2017-12-18
    战区日统计
    """

    __tablename__ = 'war_zone_daily_statics'

    filter_dict = {
        'war_zone_id': (),
        'date_begin': ('date', '>='),
        'date_end': ('date', '<=')
    }

    # 战区id
    war_zone_id = db.Column(db.ForeignKey('war_zone.id'), index=True)
    # 统计数据的日期
    date = db.Column(db.Date(), default=date.today, index=True)
    # 销售额
    sales = db.Column(
        db.DECIMAL(10, 2), default=0, server_default=text("'0'")
    )
    # 付费人数
    pay_people_cnt = db.Column(
        db.Integer(), default=0, server_default=text("'0'")
    )
    # 总订单数
    ro_cnt = db.Column(db.Integer(), default=0, server_default=text("'0'"))
    # 总发布数(NOTE: 因为其他地方都用的published_count, 所以这里同步而没用cnt)
    published_count = db.Column(db.Integer, default=0, server_default=text("'0'"))
    # 总抢单次数
    bid_cnt = db.Column(db.Integer(), default=0, server_default=text("'0'"))
    # 总抢单人数
    bid_people_cnt = db.Column(
        db.Integer(), default=0, server_default=text("'0'")
    )
    # 总锁定次数
    locked_cnt = db.Column(db.Integer(), default=0, server_default=text("'0'"))
    # 总派单人数
    dispatch_people_cnt = db.Column(
        db.Integer(), default=0, server_default=text("'0'")
    )
    # 消耗派单点
    cost_dispatch_point = db.Column(
        db.Integer(), default=0, server_default=text("'0'")
    )
    # 消耗汇币数
    cost_bid_point = db.Column(
        db.Integer(), default=0, server_default=text("'0'")
    )
    # 消费人数
    cost_bid_people_cnt = db.Column(
        db.Integer(), default=0, server_default=text("'0'")
    )
    # 退返汇币
    return_bid_point = db.Column(
        db.Integer(), default=0, server_default=text("'0'")
    )
    # 退返派单点
    return_dispatch_point = db.Column(
        db.Integer(), default=0, server_default=text("'0'")
    )
    # 退返人数
    return_bid_people_cnt = db.Column(
        db.Integer(), default=0, server_default=text("'0'")
    )
    # 退返派单人数
    return_dispatch_people_cnt = db.Column(
        db.Integer(), default=0, server_default=text("'0'")
    )
    # 总人数（战区人数）
    people_cnt = db.Column(db.Integer(), default=0, server_default=text("'0'"))
    # 取关人数
    cancel_attend_cnt = db.Column(
        db.Integer(), default=0, server_default=text("'0'")
    )
    # 新增绑定人数
    attend_cnt = db.Column(db.Integer(), default=0, server_default=text("'0'"))

    war_zone = db.relationship('WarZone')
